package lumis.service.pagepersonalization.doui.processactionhandler;

import lumis.doui.processaction.ProcessActionHandler;
import lumis.doui.source.Source;
import lumis.doui.source.field.ISourceField;
import lumis.portal.PortalException;
import lumis.portal.manager.ManagerFactory;
import lumis.service.pagepersonalization.bean.PagePersonalizationHolderConfiguration;
import lumis.service.pagepersonalization.bean.PagePersonalizationHolderConfigurationPK;
import lumis.service.pagepersonalization.bean.PagePersonalizationHolderConfiguration.Type;
import lumis.util.XmlUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * {@link ProcessActionHandler} responsible for activating or reverting
 * temporary configuration.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class ActivateRevertDefaultConfigurationProcessActionHandler extends ProcessActionHandler<Source<ISourceField>>
{
	public void processAction() throws PortalException
	{
		String action = XmlUtil.readAttributeString("action", processActionNode);
		boolean activate = action != null && action.equals("activate");

		String pageId = douiContext.getRequest().getPageConfig().getId();
		List<String> affectedIds = new ArrayList<String>(3);
		Collection<String> interfaceInstanceIds = ManagerFactory.getServiceInterfaceInstanceManager().getIdsByPageId(sessionConfig, pageId,
				transaction);
		for (String interfaceInstanceId : interfaceInstanceIds)
		{
			String serviceInterfaceId = ManagerFactory.getServiceInterfaceInstanceManager().get(sessionConfig, interfaceInstanceId,
					transaction).getServiceInterfaceId();
			if (serviceInterfaceId.equals(douiContext.getRequest().getServiceConfig().getId() + ".widgetHolder"))
				affectedIds.add(interfaceInstanceId);
		}

		if (activate)
		{
			doAction(affectedIds, Type.Temporary, Type.Default);
		}
		else
		{
			doAction(affectedIds, Type.Default, Type.Temporary);
		}

		addDefaultResponse();
	}

	private void doAction(List<String> affectedIds, Type origType, Type dstType) throws PortalException
	{
		for (String serviceInterfaceInstanceId : affectedIds)
		{
			PagePersonalizationHolderConfiguration org = PagePersonalizationHolderConfiguration.getByTypeAndHolderInstanceId(origType, serviceInterfaceInstanceId);
	
			if (org == null)
				continue;
	
			PagePersonalizationHolderConfiguration dst = PagePersonalizationHolderConfiguration.getByTypeAndHolderInstanceId(dstType, serviceInterfaceInstanceId);
			if (dst == null)
			{
				dst = new PagePersonalizationHolderConfiguration();
				PagePersonalizationHolderConfigurationPK pk = new PagePersonalizationHolderConfigurationPK();
				pk.setHolderInstanceId(serviceInterfaceInstanceId);
				pk.setTypeOfConf(dstType.getSqlType());
				dst.setId(pk);
			}
			dst.setWidgets(org.getWidgets());
			ManagerFactory.getEntityManager().persist(dst);
		}
	}
}
